package com.example.chenjian.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by chenjian on 2017/1/7.
 */


public class GoodsListAdapter extends ArrayAdapter<GoodsItem> {
    public GoodsListAdapter(Context context, int source, List<GoodsItem> goodsItems) {
        super(context, source, goodsItems);
        this.mResource = source;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final GoodsItem item = getItem(position);

        View view = null;
        ViewHolder viewHolder = null;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(mResource, null);

            viewHolder = new ViewHolder();
            viewHolder.goodsImageView = (ImageView) view.findViewById(R.id.goods_img);
            viewHolder.goodsNameView = (TextView) view.findViewById(R.id.goods_name);
            viewHolder.goodsSaleView = (TextView) view.findViewById(R.id.goods_sale);
            view.setTag(viewHolder);
        }else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.goodsNameView.setText(item.getName());
        viewHolder.goodsSaleView.setText(item.getSaleDes());

        return view;
    }

    private int mResource;

    class ViewHolder {
        ImageView goodsImageView;
        TextView goodsNameView;
        TextView goodsSaleView;
    }
}
